package com.partners.controller;

import com.partners.annotation.BeanValid;
import com.partners.annotation.SimpleParam;
import com.partners.constant.ErrorMessageEnum;
import com.partners.constant.PermissionEnum;
import com.partners.constant.SuccessMessageEnum;
import com.partners.dao.dto.RoleInputDTO;
import com.partners.model.ApiResult;
import com.partners.model.BusinessException;
import com.partners.model.Paging;
import com.partners.service.RoleService;
import com.partners.service.UserService;
import com.partners.utils.ApiResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author liangyongle
 * @date 2018-12-07
 */
@RequestMapping("role")
@RestController
@Slf4j
public class RoleController {

    @Resource
    private RoleService roleService;

    @Resource
    private UserService userService;


    /**
     * 获取相应角色列表
     * @param
     * @return List
     *
     */
    @PostMapping("getRole")
    public ApiResult getRoleList(@SimpleParam("id") Integer pid,
                                 @SimpleParam("uid") Integer uid,
                                 @BeanValid @RequestBody Paging paging){
        if(uid == null){
            return ApiResultUtil.failure(ErrorMessageEnum.UID_NOT_FOUND);
        }
        if(pid == null){
            return ApiResultUtil.failure(ErrorMessageEnum.ID_NOT_FOUND);
        }
        Integer code = PermissionEnum.ROLE_LIST.getCode();
        userService.isPermission(uid, code);
        //List<Map<String,Object>> list =  roleService.getMyRoleList(pid);
        Map<String,Object> list = roleService.getMyRoleList(paging,pid);
        return ApiResultUtil.ok(list);
    }

    /**
     * 获取角色详情
     * @param Id Integer
     * @return Object
     */
    @PostMapping("role_detail")
    public ApiResult getRoleDetail(@SimpleParam("id") Integer Id,@SimpleParam("uid") Integer uid){
        if(Id == null){ throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);}
//        if(uid == null){ throw BusinessException.of(ErrorMessageEnum.UID_NOT_FOUND);}
//        Integer code = PermissionEnum.ROLE_DETAIL.getCode();
//        userService.isPermission(uid, code);
        Map<String,Object> role = roleService.getRoleDetail(Id);
        return ApiResultUtil.ok(role);
    }


    /**
     * 获取角色列表
     * @param
     * @return List
     *
     */
    @PostMapping("role_list")
    public ApiResult getRoleList(@BeanValid @RequestBody Paging paging,
                                 @SimpleParam("name") String name){
        Map<String,Object> list =  roleService.getRoleList(paging,name );
        return ApiResultUtil.ok(list);
    }


    /**
     * 获取角色详情
     * @param roleInputDTO RoleInputDTO
     * @return Object
     */
    @PostMapping("role_edit")
    public ApiResult EditRole(@RequestBody RoleInputDTO roleInputDTO){
        if(roleInputDTO.getUid() == null){
            throw BusinessException.of(ErrorMessageEnum.UID_NOT_FOUND);
        }
        Integer code = PermissionEnum.ROLE_EDIT.getCode();
        userService.isPermission(roleInputDTO.getUid(), code);
        roleService.editRole(roleInputDTO);
        return ApiResultUtil.ok(0);
    }


    /**
     * 获取角色详情
     * @param Id Integer
     * @return Object
     */
    @PostMapping("role_delete")
    public ApiResult roleDelete(@SimpleParam("id") Integer Id,@SimpleParam("uid") Integer uid){
        if(Id == null){
            throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        if(uid == null){
            throw BusinessException.of(ErrorMessageEnum.UID_NOT_FOUND);
        }
        Integer code = PermissionEnum.ROLE_DELETE.getCode();
        userService.isPermission(uid, code);
        Integer result = roleService.deleteRole(Id);
        return ApiResultUtil.ok(result);
    }


    /**
     * 获取角色下面的用户列表
     * @param Id Integer
     * @return Object
     */
    @PostMapping("role_user")
    public ApiResult roleUser(@BeanValid @RequestBody Paging paging,@SimpleParam("id") Integer Id){
        Map<String,Object> list =  roleService.getRoleUserList(paging,Id);
        return ApiResultUtil.ok(list);
    }





}
